package com.lkd.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lkd.constant.StatusConstant;
import com.lkd.dto.LoginDto;
import com.lkd.entity.User;
import com.lkd.exception.BusinessException;
import com.lkd.exception.GlobalExceptionHandler;
import com.lkd.mapper.LoginMapper;
import com.lkd.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    /**
     * 登录
     * @param dto
     * @return
     */
    @Override
    public User login(LoginDto dto) {
        //1.验证用户名密码
        String loginName = dto.getLoginName();
        String password = dto.getPassword();
        //2.查验用户名和密码是否存在
        if(StrUtil.isEmpty(loginName)||StrUtil.isEmpty(password)){
            throw  new BusinessException("用户名或密码不能为空");
        }
        //3.登录
        User user = loginMapper.login(loginName);
        // 3.业务校验
        // 3-1 用户是否存在
        if (user == null) {
            throw new BusinessException("用户名不存在");
        }

        // 3-2 密码是否正确
        if (!StrUtil.equals(password, user.getPassword())) {
            throw new BusinessException("密码错误");
        }

        if (!(user.getStatus()==1)){
            throw new BusinessException("用户已被禁用");
        }

        // 4.登录成功
        return user;

    }
}
